{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8e0d47c",
   "metadata": {},
   "source": [
    "<style>\n",
    ".small-text {\n",
    "    font-size: 12px;\n",
    "}\n",
    ".medium-text {\n",
    "    font-size: 16px;\n",
    "}\n",
    "</style>\n",
    "\n",
    "<p class=\"small-text\">这是小号字体</p>\n",
    "<p class=\"medium-text\">这是中等字体</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "440626f3",
   "metadata": {},
   "source": [
    "继承\n",
    "注意：在继承中基类的构造（  init （）方法）属性不会被自动调用，它需要在其派生类的构造中亲自专门调用。\n",
    "·super（）. init  （参数1, 参数2, ....）\n",
    "·父类名称. init   (self,参数1, 参数2,…)\n",
    "\n",
    "继承后，子类调用的方法，首先从本类中查找，如果没有就从父类中查找，还没有就从更高类中查找，如果都没有，报错！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b607f30b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#继承\n",
    "class Father:\n",
    "    def __init__(self):\n",
    "        self.name = '张三'\n",
    "        print('lots of money')\n",
    "    \n",
    "    def play(self):\n",
    "        print('pay money for service')\n",
    "    \n",
    "    def eat(self):\n",
    "        print('pay money for delicacies.')\n",
    "\n",
    "class Son(Father):\n",
    "    def __init__(self):\n",
    "        Father.__init__(self)  # 或 super().__init__()\n",
    "        self.son = '李四'\n",
    "        print(self.son, '的老子是', self.name)\n",
    "        print('i want to make money by myself')\n",
    "    \n",
    "    def work(self):\n",
    "        print('work for money')\n",
    "    \n",
    "    def dream(self):\n",
    "        print('run for dream in life')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddd8a7e1",
   "metadata": {},
   "source": [
    "多态：不同的对象，使用相同的方法时表现出不同的状态。\n",
    "1.概念\n",
    "一种事物的多种体现形式，函数的重写其实就是多态的一种体现。\n",
    "在Python中，多态指的是父类的引用指向子类的对象\n",
    "\n",
    "2.Python中，多态指的是父类引用指向子类对象。Python中，实现多态的基本条件：\n",
    "1）要有继承（子类、父类）\n",
    "2）要有方法的重写\n",
    "3）父类引用子类对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e87db9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 父类\n",
    "class Animal(object):\n",
    "    pass\n",
    "\"\"\"如果父类不需要继承object：\n",
    "class Animal:\n",
    "    pass\n",
    "\"\"\"\n",
    "# 子类\n",
    "class Dog(Animal):\n",
    "    pass\n",
    "class Cat(Animal):\n",
    "    pass\n",
    "# 定义变量\n",
    "a = Animal()  # a是Animal类型\n",
    "b = Dog()    # b是Dog类型\n",
    "c = Cat()    # c是Cat类型\n",
    "# 类型检查\n",
    "print(isinstance(c, Animal))  # True\n",
    "print(isinstance(a, Dog))     # False"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
